#!/bin/bash
[ $# -eq 0 ] && { echo "Usage: $0 LANGUAGE WORDSFILE"; exit 1; }

LANG=C
LANGUAGE=$1
WORDSFILE=$2

mkdir Test-$LANGUAGE

cat $WORDSFILE | while read WORD
do
	expr_path=`pwd`

	echo $WORD
	WORD_DIR=$expr_path/Test-$LANGUAGE/$WORD
	
	mkdir -p $WORD_DIR

	cd $WORD_DIR
		
		# PREPARE DATA
		# Remove all $WORD_DIR content
		#echo "Removing all data inside $WORD_DIR "
		#rm -r $WORD_DIR/*

		# Copy Regexs folder into $WORD_DIR
		cp -r $expr_path/Regexs-$LANGUAGE ./Regexs

		echo "Preparing data for experiment execution"
		# Select instances of WORD and tag corpus with <head>$WORD</head>
		$expr_path/myscripts/SLacquis2plain.sh $expr_path/JRC-Acquis/alignedCorpus_$LANGUAGE-it.xml tagged_data.txt $WORD
		
		# Convert plain text to sval2 format
		text2sval.pl tagged_data.txt > $WORD-corpus.xml
		rm tagged_data.txt

		# divide corpus into training and test parts	
		preprocess.pl --token Regexs/token.regex --removeNotToken --split 95 $WORD-corpus.xml

		# Create the target.regex
		maketarget.pl -head LEXELT-test.xml
		mv target.regex Regexs/target.regex
		
		#remove unused files
		rm LEXELT-test.xml.* LEXELT-test.count
		rm LEXELT-training.xml
	cd ../..
done